package demo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 */
public class UsedCard {
    //静态成员方法
    public static final String[] suits ={"♥","♠","♣","♦"};
    //定义牌
    public static List<Card> isCard(){
        //创建了一个初始容量为 52 的动态列表，用于存储 Card 对象
        List<Card> cards = new ArrayList<>(52);
        //遍历1-13的点数
        for (int i = 1; i <= 13; i++) {
            //遍历 4 种花色
            for (int j = 0; j < 4; j++) {
                int rank = i;
                String  suit = suits[j];
                //创建Card对象
                Card card = new Card(rank,suit);
                card.rank = rank;
                card.suit = suit;
                //将card添加到cards列表中
                cards.add(card);
            }
        }
        return cards;
    }
    //洗牌
    public static void shuffle(List<Card> cards){
        Random random = new Random();
        //遍历
        for (int i = cards.size()-1; i > 0 ; i--) {
            //生成一个[0,i)的随机数
            int index = random.nextInt(i);
            //将下标i和生成随机数的下标进行交换
            swap(cards,i,index);
        }
    }
    //两个牌进行交换
    private static void swap(List<Card> cards , int m ,int n){
        //获得m下标的值
        Card tmp = cards.get(m);
        //将下标n的值替换到m下标中
        cards.set(m,cards.get(n));
        //将tmp(以前下标m的值)替换到n下标中
        cards.set(n,tmp);
    }
}
